package com.badibadi.uniclubber;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.badibadi.abcpinying.CharacterParser;
import com.badibadi.abcpinying.ClearEditText;
import com.badibadi.abcpinying.PinyinComparator_Friends;
import com.badibadi.abcpinying.SideBar;
import com.badibadi.abcpinying.SideBar.OnTouchingLetterChangedListener;
import com.badibadi.activity.AddFriendValidationActivity;
import com.badibadi.activity.BaseActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.activity.SeeOtherPeopleSpaceActivity;
import com.badibadi.infos.MyFriendsModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.CommonUtils;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.libjph.util.JsonUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

/**
 * 看俱乐部会员：2014-10-14 和转让俱乐部都可以用 只需要设置标记flags就行了 myfriendresult_index
 * 
 * @author Administrator
 * 
 */
public class PinyinSeeClubMemberJCZYActivity extends BaseActivity implements
		OnClickListener, IXListViewListener {
	private static final String TAG = "PinyinSeeClubMemberJCZYActivity";
	private Button myfriendresult_back;// 返回
	private Button myfriendresult_index;// 完成
	private XListView xListView;
	private Results results;
	private int Page;
	private List<MyFriendsModel> list;
	private MyFriendResult_Adapter adapter;
	// private SeeOtherPeopleAdapter seeClubMemberAdapter;//看俱乐部会员使用他
	private SideBar sideBar;
	private TextView dialog;
	private CheckBox myfriendresult_checkall;
	private ClearEditText myfriendresult_edit;
	/**
	 * 临时存储@的好友名字 myfriendresult_index
	 */
	// public static Map<Integer, String> strings = new HashMap<Integer,
	// String>();
	public List<MyFriendsModel> friendsModels;
	/**
	 * 汉字转换成拼音的类
	 */
	private CharacterParser characterParser;

	/**
	 * myfriendresult_index 根据拼音来排列ListView里面的数据类
	 */
	private PinyinComparator_Friends pinyinComparator;
	private String flags = "0";// ;0是看俱乐部会员，1是转让俱乐部管理员//这只是界面的
	private String cid = "11";
	private String type = "2";// 1:俱乐部转让 2：俱乐部会员总览列表//这是接口的
	private String uid = "1";
	private String cj_uid = "1";// 俱乐部管理员的id

	private int select_num = 0;// 选择的个数
	private int up_select_num = -1;// 上一个选择项

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(PinyinSeeClubMemberJCZYActivity.this);
					Utils.showMessage(PinyinSeeClubMemberJCZYActivity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				offload();
				break;
			case 2:
				try {
					Utils.ExitPrgress(PinyinSeeClubMemberJCZYActivity.this);
				} catch (Exception e) {

				}
				// Constants.friendsModels.addAll(list);
				// Constants.friendsModels =
				// filledData(Constants.friendsModels);
				friendsModels.addAll(list);
				friendsModels = filledData(friendsModels);

				// 根据a-z进行排序源数据
				Collections.sort(friendsModels, pinyinComparator);
				// for (int i = 0; i < friendsModels.size(); i++) {
				// (""
				// + friendsModels.get(i).getSortLetters());
				// }

				adapter.notifyDataSetChanged();
				try {
					offload();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {

					Utils.ExitPrgress(PinyinSeeClubMemberJCZYActivity.this);
					Utils.showMessage(PinyinSeeClubMemberJCZYActivity.this,
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}
				offload();
				break;
			case 4:
				try {

					Utils.ExitPrgress(PinyinSeeClubMemberJCZYActivity.this);
					Utils.showMessage(PinyinSeeClubMemberJCZYActivity.this,
							getResources().getString(R.string.SystemError));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 5:
				try {

					Utils.ExitPrgress(PinyinSeeClubMemberJCZYActivity.this);
					Utils.showMessage(PinyinSeeClubMemberJCZYActivity.this,
							getResources().getString(R.string.l_xb177));
					finish();
				} catch (Exception e) {
					// TODO: handle exception
				}
				// 转让成功
				break;
			case 6:

				try {

					Utils.ExitPrgress(PinyinSeeClubMemberJCZYActivity.this);
					Utils.showMessage(PinyinSeeClubMemberJCZYActivity.this,
							getResources().getString(R.string.l_xb178));
					finish();
				} catch (Exception e) {
					// TODO: handle exception
				}
				// 转让失败
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		goToTheTitle();
		setContentView(R.layout.myfriendresult_main);
		flags = getIntent().getStringExtra("flags");// 标记
		cid = getIntent().getStringExtra("cid");
		cj_uid = getIntent().getStringExtra("cj_uid");
		uid = Utils.getUid(PinyinSeeClubMemberJCZYActivity.this);
		// cj_uid=getIntent().getStringExtra("cj_uid");//浏览俱乐部的时候用到（管理员的id）//
		// 看俱乐部会员的时候才传
		if (flags == null) {
			flags = "0";// 默认是看俱乐部会员
		}

		if (flags.equals("0")) {
			type = "2";
		} else {
			type = "1";
		}

		Page = 0;
		init();
	}

	private void init() {
		if (flags.equals("0"))// 看俱乐部会员的
		{
			// layout_marginTop
			this.findViewById(R.id.myfriendresult_line2).setVisibility(
					View.GONE);
			FrameLayout.LayoutParams layoutParam = new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.MATCH_PARENT);
			this.findViewById(R.id.myfriendresult_crew_ll).setLayoutParams(
					layoutParam);
			this.findViewById(R.id.myfriendresult_index).setVisibility(
					View.GONE);
			this.findViewById(R.id.myfriendresult_quanxuan_quanbuxuan)
					.setVisibility(View.GONE);
			this.findViewById(R.id.ll_et).setVisibility(View.GONE);
			((TextView) this.findViewById(R.id.myfriendresult_tital))
					.setText(getResources().getString(R.string.ClubMember));

		} else// 转让
		{
			FrameLayout.LayoutParams layoutParam = new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT,
					FrameLayout.LayoutParams.MATCH_PARENT);
			this.findViewById(R.id.myfriendresult_crew_ll).setLayoutParams(
					layoutParam);
			this.findViewById(R.id.myfriendresult_line2).setVisibility(
					View.GONE);
			this.findViewById(R.id.myfriendresult_quanxuan_quanbuxuan)
					.setVisibility(View.GONE);
			((TextView) this.findViewById(R.id.myfriendresult_tital))
					.setText(getResources().getString(R.string.l_xb179));
		}

		myfriendresult_back = (Button) findViewById(R.id.myfriendresult_back);
		myfriendresult_checkall = (CheckBox) findViewById(R.id.myfriendresult_checkall);
		myfriendresult_edit = (ClearEditText) findViewById(R.id.myfriendresult_edit);
		friendsModels = new ArrayList<MyFriendsModel>();
		// Constants.strings = new HashMap<Integer, String>();
		myfriendresult_index = (Button) findViewById(R.id.myfriendresult_index);
		adapter = new MyFriendResult_Adapter(friendsModels,
				getApplicationContext());
		xListView = (XListView) findViewById(R.id.myfriendresult_list);
		xListView.setXListViewListener(this);
		xListView.setPullLoadEnable(true);
		xListView.setPullRefreshEnable(true);
		xListView.setAdapter(adapter);
		xListView.startLoadMore();
		// xListView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent(
		// PinyinSeeClubMemberJCZYActivity.this,
		// SeeOtherPeopleSpaceActivity.class);
		// if (arg2 > 0) {
		// intent.putExtra("friend_id", friendsModels.get(arg2 - 1)
		// .getId());
		// } else {
		// intent.putExtra("friend_id", friendsModels.get(arg2)
		// .getId());
		// }
		// startActivity(intent);
		// // Utils.showMessage(PinyinSeeClubMemberJCZYActivity.this, arg2
		// // + "  " + friendsModels.size());
		// }
		// });
		myfriendresult_back.setOnClickListener(this);
		myfriendresult_index.setOnClickListener(this);
		// 实例化汉字转拼音类
		characterParser = CharacterParser.getInstance();
		pinyinComparator = new PinyinComparator_Friends();
		sideBar = (SideBar) findViewById(R.id.myfriendresult_sidrbar);
		dialog = (TextView) findViewById(R.id.myfriendresult_dialog);
		sideBar.setTextView(dialog);

		myfriendresult_edit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				filterData(arg0.toString());
				if (arg0.toString().length() == 0) {
					onRefresh();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});

		// 设置右侧触摸监听
		sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

			@Override
			public void onTouchingLetterChanged(String s) {
				// 该字母首次出现的位置
				// ("出现位置:" + s.charAt(0));
				int position = adapter.getPositionForSection(s.charAt(0));
				if (position != -1) {
					xListView.setSelection(position);
				}
			}
		});
		myfriendresult_checkall
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean arg1) {
						// TODO Auto-generated method stub
						if (arg1) {
							adapter.setAllcheck_OK();
						} else {
							adapter.setAllcheck_NO();
						}
					}
				});
	}

	private void Friends(final String cid, final String uid, final int page,
			final String type) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<MyFriendsModel>();
				Map<String, Object> map = new HashMap<String, Object>();

				map.put("cid", cid);
				map.put("uid", uid);
				map.put("page", page);
				if (flags.equals("0")) {
					map.put("type", type);
				} else {
					map.put("type", type);
				}
				map.put("pageNum", 999);
				// ("查看俱乐部会员列表提交的数据是:" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "club/menber");
				// String content = Utils.getRequest(Constants.BadiUrl_1
				// +
				// "club/menber?cid=+cid"&uid=" + uid + "&page=" + page + "&pageNum="
				// + 10);
				// (Constants.BadiUrl + "friends?uid=" + id
				// + "&page=" + page + "&pageNum=" + 10);
				if (content != null) {
					results = Utils.checkResult_NNN(getApplicationContext(),
							content);
					if (results != null && !results.getRetmsg().equals("null")) {
						// ("查看俱乐部会员列表返回的数据是:" + content);
						try {
							list = JsonUtil.getListByJsonString(
									results.getRetmsg(), MyFriendsModel.class);
							handler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block

						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						handler.sendEmptyMessage(3);
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private void offload() {
		xListView.stopRefresh();
		xListView.stopLoadMore();
	}

	/**
	 * 根据输入框中的值来过滤数据并更新ListView
	 * 
	 * @param filterStr
	 */
	private void filterData(String filterStr) {
		List<MyFriendsModel> filterDateList = new ArrayList<MyFriendsModel>();
		// 是否为空
		if (TextUtils.isEmpty(filterStr)) {
			filterDateList = friendsModels;
		} else {
			filterDateList.clear();
			for (MyFriendsModel sortModel : friendsModels) {
				String name = sortModel.getNickName();
				if (name.indexOf(filterStr.toString()) != -1
						|| characterParser.getSelling(name).startsWith(
								filterStr.toString())) {
					filterDateList.add(sortModel);
				}
			}
		}

		// 根据a-z进行排序
		Collections.sort(filterDateList, pinyinComparator);
		adapter.updateListView(filterDateList);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.myfriendresult_back:// 返回
			if (friendsModels.size() > 0) {
				friendsModels.clear();
			}
			this.finish();
			break;
		case R.id.myfriendresult_index:// 完成

			if (friendsModels != null) {
				for (int i = 0; i < friendsModels.size(); i++) {

					if (friendsModels.get(i).isJudge()) {
						if (friendsModels.get(i).getUid().equals(uid)) {
							Utils.showMessage(
									PinyinSeeClubMemberJCZYActivity.this,
									getResources().getString(R.string.l_xb180));
							break;
						} else// 开始转让
						{
							// startZhuanRang(friendsModels.get(i).getId());
							startZhuanRang(friendsModels.get(i).getUid());
							break;
						}
					}
				}
			}

			// Intent it = new Intent();
			// it.putExtra("result", (Serializable) friendsModels);
			// setResult(3, it);
			// finish();
			break;

		default:
			break;
		}
	}

	// 开始转让
	/**
	 * 转让给谁
	 * 
	 * @param toUid
	 */
	private void startZhuanRang(String toUid) {
		// TODO Auto-generated method stub
		// club/assignClub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("toUid", toUid);
		map.put("cid", cid);
		CommonUtils.CommomPostfunction(PinyinSeeClubMemberJCZYActivity.this,
				Constants.BadiUrl_1 + "club/assignClub", map, handler, 1, 4, 5,
				6);

	}

	private class MyFriendResult_Adapter extends BaseAdapter {
		protected static final String TAG = "MyFriendResult_Adapter";
		private DisplayImageOptions options;
		Context context;

		public MyFriendResult_Adapter(List<MyFriendsModel> friendsModels,
				Context context) {
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_stub)
					.showImageForEmptyUri(R.drawable.noimage)
					.showImageOnFail(R.drawable.noimage).cacheInMemory()
					.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
					.build();
			this.context = context;
		}

		/**
		 * 当ListView数据发生变化时,调用此方法来更新ListView
		 * 
		 * @param list
		 */
		public void updateListView(List<MyFriendsModel> list) {
			friendsModels = list;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return friendsModels.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return friendsModels.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			// LayoutInflater inflater = LayoutInflater.from(context);
			// arg1 = inflater.inflate(R.layout.myfriendresult_item, null);
			if (flags.equals("1")) {

				if (arg1 == null) {
					arg1 = com.badibadi.mytools.ViewHolder.buildView(
							getApplicationContext(),
							R.layout.myfriendresult_item, arg2);
				}
				com.badibadi.mytools.ViewHolder viewHolder = (com.badibadi.mytools.ViewHolder) arg1
						.getTag();
				final ImageView myfriendresult_item_head = viewHolder
						.getViewById(R.id.myfriendresult_item_head);
				RelativeLayout myfriendresult_item_rel = viewHolder
						.getViewById(R.id.myfriendresult_item_rel);
				TextView myfriendresult_item_name = viewHolder
						.getViewById(R.id.myfriendresult_item_name);
				final CheckBox myfriendresult_item_ok = viewHolder
						.getViewById(R.id.myfriendresult_item_ok);
				RelativeLayout RL_MycheckBox = viewHolder
						.getViewById(R.id.RL_MycheckBox);

				myfriendresult_item_ok.setFocusable(false);
				myfriendresult_item_ok.setFocusableInTouchMode(false);

				if (flags.equals("0")) {
					RL_MycheckBox.setVisibility(View.GONE);
				}
				// ("图片地址是:" + Constants.BadiDownImgUrl
				// + friendsModels.get(arg0).getHead());
				// ImageLoader.getInstance().loadImage(
				// Constants.BadiDownImgUrl
				// + friendsModels.get(arg0).getHead()+Constants.appPhoto4img,
				// options,
				// new ImageLoadingListener() {
				//
				// @Override
				// public void onLoadingStarted(String arg0, View arg1) {
				// // TODO Auto-generated method stub
				//
				// }
				//
				// @Override
				// public void onLoadingFailed(String arg0, View arg1,
				// FailReason arg2) {
				// // TODO Auto-generated method stub
				//
				// }
				//
				// @Override
				// public void onLoadingComplete(String arg0,
				// View arg1, Bitmap arg2) {
				// // double dd1 = itemWidth
				// // / ((double) ((float) (arg2.getWidth()) / arg2
				// // .getHeight()));
				// myfriendresult_item_head.setImageBitmap(Utils
				// .scaleImg(arg2, 94, 94));
				// }
				//
				// @Override
				// public void onLoadingCancelled(String arg0,
				// View arg1) {
				// // TODO Auto-generated method stub
				//
				// }
				// });

				ImageLoader.getInstance().displayImage(
						Constants.BadiDownImgUrl
								+ friendsModels.get(arg0).getHead()
								+ Constants.appPhoto4img,
						myfriendresult_item_head, options);
				// Constants.BadiDownImgUrl
				// + friendsModels.get(arg0).getHead()+Constants.appPhoto4img,
				// options,
				// new ImageLoadingListener() {
				myfriendresult_item_name.setText(friendsModels.get(arg0)
						.getNickName());
				myfriendresult_item_ok.setChecked(friendsModels.get(arg0)
						.isJudge());
				myfriendresult_item_rel
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View arg012) {
								if (flags.equals("1"))// 如果是转让的话
								{
									if (myfriendresult_item_ok.isChecked()) {
									} else {
										myfriendresult_item_ok.setChecked(true);
										friendsModels.get(arg0).setJudge(true);
										if (up_select_num != -1) {
											friendsModels.get(up_select_num)
													.setJudge(false);
											notifyDataSetChanged();
											// myfriendresult_item_ok.setChecked(false);
										}
										up_select_num = arg0;
									}
									// if (myfriendresult_item_ok.isChecked()) {
									// myfriendresult_item_ok.setChecked(false);
									// friendsModels.get(arg0).setJudge(false);
									// // Constants.strings.put(arg0, null);
									// } else {
									// myfriendresult_item_ok.setChecked(true);
									// friendsModels.get(arg0).setJudge(true);
									// // Constants.strings.put(arg0, "@"
									// // + Constants.friendsModels.get(arg0)
									// // .getNickName() + " ");
									// }
								}

							}
						});

			} else {
				arg1 = SeeClubMember(context, arg0, arg1);// final int arg0,
															// View arg1,
															// ViewGroup arg2
			}
			return arg1;
		}

		// /**
		// * 根据ListView的当前位置获取分类的首字母的Char ascii值
		// */
		// public int getSectionForPosition(int position) {
		// return friendsModels.get(position).getSortLetters().charAt(0);
		// }

		public View SeeClubMember(final Context mContext, final int position,
				View view) {
			ViewHolder viewHolder = null;
			final MyFriendsModel mContent = friendsModels.get(position);
			// 这里可以重写
			if (view == null) {
				viewHolder = new ViewHolder();
				view = LayoutInflater.from(mContext).inflate(
						R.layout.abc_see_other_people_item, null);
				viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
				viewHolder.tvLetter = (TextView) view
						.findViewById(R.id.catalog);
				viewHolder.is_guanliyuan = (TextView) view
						.findViewById(R.id.is_guanliyuan);
				viewHolder.isFriends = (TextView) view
						.findViewById(R.id.isFriends);
				viewHolder.motto = (TextView) view.findViewById(R.id.motto);
				viewHolder.head = (ImageView) view.findViewById(R.id.head);
				view.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}

			// 根据position获取分类的首字母的Char ascii值
			int section = getSectionForPosition(position);

			// 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
			if (position == getPositionForSection(section)) {
				viewHolder.tvLetter.setVisibility(View.VISIBLE);
				viewHolder.tvLetter.setText(mContent.getSortLetters());
			} else {
				viewHolder.tvLetter.setVisibility(View.GONE);
			}
			viewHolder.tvTitle.setText(friendsModels.get(position)
					.getNickName());
			viewHolder.tvTitle.setTextColor(getResources().getColor(
					R.color.black));
			if (friendsModels.get(position).getUid() != null
					&& friendsModels.get(position).getUid() != null) {
				// ("参与者id" + list.get(position).toString());
				if (cj_uid != null
						&& friendsModels.get(position).getUid().equals(cj_uid)) {
					viewHolder.is_guanliyuan.setVisibility(View.VISIBLE);
					viewHolder.is_guanliyuan.setText(getResources().getString(
							R.string.l_xb181));
				} else {
					viewHolder.is_guanliyuan.setVisibility(View.GONE);
				}
				if (friendsModels.get(position).getUid()
						.equals(Utils.getUid(mContext))) {
					viewHolder.isFriends.setText(getResources().getString(
							R.string.l_xb182));
					viewHolder.isFriends.setTextColor(mContext.getResources()
							.getColor(R.color.gray_9));
				} else {
					if (mContent.getIs_firend().equals("1")) {
						viewHolder.isFriends.setText(getResources().getString(
								R.string.Friends));
						viewHolder.isFriends.setTextColor(mContext
								.getResources().getColor(R.color.gray_9));
					} else {
						viewHolder.isFriends.setText(getResources().getString(
								R.string.AddFriend));
						viewHolder.isFriends.setTextColor(mContext
								.getResources().getColor(R.color.green));
						viewHolder.isFriends
								.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										if (!Utils.getUid(context).equals("")) {
											// 在这里提交加为好友的申请
											Intent intent = new Intent(
													mContext,
													AddFriendValidationActivity.class);
											intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
											intent.putExtra("id", friendsModels
													.get(position).getUid());
											mContext.startActivity(intent);
										} else {
											Toast.makeText(
													context,
													context.getResources()
															.getString(
																	R.string.l_xb10),
													0).show();
										}
									}
								});
					}
				}
			} else {
				// ("参与者uid" + list.get(position).getUid());
				if (cj_uid != null
						&& friendsModels.get(position).getUid().equals(cj_uid)) {
					viewHolder.is_guanliyuan.setVisibility(View.VISIBLE);
					viewHolder.is_guanliyuan.setText(getResources().getString(
							R.string.l_xb181));
				} else {
					viewHolder.is_guanliyuan.setVisibility(View.GONE);
				}
				if (friendsModels.get(position).getUid() != null
						&& friendsModels.get(position).getUid()
								.equals(Utils.getUid(mContext))) {
					viewHolder.isFriends.setText(getResources().getString(
							R.string.l_xb182));
					viewHolder.isFriends.setTextColor(mContext.getResources()
							.getColor(R.color.gray_9));
				} else {
					if (mContent.getIs_firend().equals("1")) {
						viewHolder.isFriends.setText(getResources().getString(
								R.string.Friends));
						viewHolder.isFriends.setTextColor(mContext
								.getResources().getColor(R.color.gray_9));
					} else {
						viewHolder.isFriends.setText(getResources().getString(
								R.string.AddFriend));
						viewHolder.isFriends.setTextColor(mContext
								.getResources().getColor(R.color.green));
						viewHolder.isFriends
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										if (!Utils.getUid(context).equals("")) {
											// 在这里提交加为好友的申请
											Intent intent = new Intent(
													mContext,
													AddFriendValidationActivity.class);
											intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
											intent.putExtra("id", friendsModels
													.get(position).getUid());
											mContext.startActivity(intent);
										} else {
											Toast.makeText(
													context,
													context.getResources()
															.getString(
																	R.string.l_xb10),
													0).show();
										}
									}
								});
					}
				}
			}
			viewHolder.motto.setVisibility(View.GONE);
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl + mContent.getHead()
							+ Constants.appPhoto4img, viewHolder.head, options);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// Intent intent = new Intent(
					// PinyinSeeClubMemberJCZYActivity.this,
					// SeeOtherPeopleSpaceActivity.class);
					// intent.putExtra("friend_id", friendsModels.get(position)
					// .getUid());
					// startActivity(intent);
					if (!friendsModels
							.get(position)
							.getUid()
							.equals(""
									+ Utils.getUid(PinyinSeeClubMemberJCZYActivity.this))) {
						Intent intent = new Intent(
								PinyinSeeClubMemberJCZYActivity.this,
								SeeOtherPeopleSpaceActivity.class);
						intent.putExtra("friend_id", friendsModels
								.get(position).getUid());
						startActivity(intent);
					} else {
						Intent intent = new Intent(
								PinyinSeeClubMemberJCZYActivity.this,
								MyDataActivity.class);
						startActivity(intent);
					}
				}
			});
			return view;
		}

		final class ViewHolder {
			TextView tvLetter;
			TextView tvTitle;
			TextView is_guanliyuan;
			TextView isFriends;
			TextView motto;
			ImageView head;
		}

		/**
		 * sdf 根据ListView的当前位置获取分类的首字母的Char ascii值
		 */
		public int getSectionForPosition(int position) {

			return friendsModels.get(position).getSortLetters().charAt(0);
		}

		/**
		 * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
		 */
		public int getPositionForSection(char section) {
			for (int i = 0; i < friendsModels.size(); i++) {
				String sortStr = friendsModels.get(i).getSortLetters();
				char firstChar = sortStr.toUpperCase().charAt(0);
				if (firstChar == section) {
					return i;
				}
			}
			return -1;
		}

		/**
		 * sdf 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
		 */
		public int getPositionForSection(int section) {

			for (int i = 0; i < getCount(); i++) {
				String sortStr = friendsModels.get(i).getSortLetters();
				char firstChar = sortStr.toUpperCase().charAt(0);
				if (firstChar == section) {
					return i;
				}
			}
			return -1;
		}

		public void setAllcheck_OK() {
			for (int i = 0; i < friendsModels.size(); i++) {
				friendsModels.get(i).setJudge(true);
				// Constants.strings.put(i, "@"
				// + Constants.friendsModels.get(i).getNickName() + " ");
				notifyDataSetChanged();
			}
		}

		public void setAllcheck_NO() {
			for (int i = 0; i < friendsModels.size(); i++) {
				friendsModels.get(i).setJudge(false);
				// Constants.strings.put(i, "@"
				// + Constants.friendsModels.get(i).getNickName() + " ");
				notifyDataSetChanged();
			}
		}

	}

	/**
	 * 为ListView填充数据
	 * 
	 * @param date
	 * @return
	 */
	private List<MyFriendsModel> filledData(List<MyFriendsModel> friends) {
		List<MyFriendsModel> friendsModels = new ArrayList<MyFriendsModel>();

		for (int i = 0; i < friends.size(); i++) {
			MyFriendsModel sortModel = new MyFriendsModel();
			sortModel.setNickName(friends.get(i).getNickName());
			sortModel.setHead(friends.get(i).getHead());
			sortModel.setId(friends.get(i).getId());
			sortModel.setUid(friends.get(i).getUid());
			sortModel.setMotto(friends.get(i).getMotto());
			sortModel.setJudge(false);
			sortModel.setIs_firend(friends.get(i).getIs_firend());
			// 汉字转换成拼音
			String pinyin = characterParser.getSelling(""
					+ friends.get(i).getNickName());
			String sortString = pinyin.substring(0, 1).toUpperCase();

			// 正则表达式，判断首字母是否是英文字母
			if (sortString.matches("[A-Z]")) {
				sortModel.setSortLetters(sortString.toUpperCase());
			} else {
				sortModel.setSortLetters("#");
			}

			friendsModels.add(sortModel);
			// ("sortModel.toString()=" + sortModel.toString());
		}
		return friendsModels;

	}

	// /**
	// * 根据输入框中的值来过滤数据并更新ListView
	// *
	// * @param filterStr
	// */
	// private void filterData(String filterStr) {
	// if (TextUtils.isEmpty(filterStr)) {
	// filterDateList = SourceDateList;
	// } else {
	// filterDateList.clear();
	// for (SortModel sortModel : SourceDateList) {
	// String name = sortModel.getName();
	// if (name.indexOf(filterStr.toString()) != -1
	// || characterParser.getSelling(name).startsWith(
	// filterStr.toString())) {
	// filterDateList.add(sortModel);
	// }
	// }
	// }
	//
	// // 根据a-z进行排序
	// Collections.sort(filterDateList, pinyinComparator);
	// adapter.updateListView(filterDateList);
	// }
	//
	// /**
	// * 根据输入框中的值来过滤数据并更新ListView
	// *
	// * @param filterStr
	// */
	// private void filterData(String filterStr) {
	// List<SortModel> filterDateList = new ArrayList<SortModel>();
	// // 是否为空
	// if (TextUtils.isEmpty(filterStr)) {
	// filterDateList = SourceDateList;
	// } else {
	// filterDateList.clear();
	// for (SortModel sortModel : SourceDateList) {
	// String name = sortModel.getName();
	// if (name.indexOf(filterStr.toString()) != -1
	// || characterParser.getSelling(name).startsWith(
	// filterStr.toString())) {
	// filterDateList.add(sortModel);
	// }
	// }
	// }
	//
	// // 根据a-z进行排序
	// Collections.sort(filterDateList, pinyinComparator);
	// adapter.updateListView(filterDateList);
	// } Motto

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		friendsModels.clear();
		up_select_num = -1;// 上一个选择项
		Page = 1;
		Friends(cid, uid, Page, type);// final String cid,final String uid,
										// final int page,final String type
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Friends(cid, uid, ++Page, type);
	}

}
